%creates 5 column matrix of force data 

%Input condition matrices (heat, walks, weights)
%1: three used actions
%2: used action in location 1
%3: used action in location 2
%4: used action in location 3
%5: unused action
%6: actual action
%7: alternative action 1
%8: alternative action 2
%9: condition

%Output numeric matrix (fulldata) column IDs:
%1: sub
%2: duration
%3: captcha score
%4: scenario
%5: condition (norm alt vs. abnorm alt)
%6: alt 1 norm (this is the critical alternative (either a normal or
%abnormal action)
%7: alt 2 norm (this is the non-critical alternative (always an abnormal action)
%8: alt 1 value 
%9: alt 2 value
%10: comp1 correct/not correct
%11: comp2 correct/not correct


%Output cell matrix column IDs:
%1: sub
%2: scenario
%3: actual action
%4: alt 1 (crit alternative)
%5: alt 2 (non-critical alternative)
%6: response to comp check 3

clear all

%specify raw data file here
load('exp2b_rawdata.mat')

%load a matrix of condition information to help organize raw data
load('exp2b_condition_matrices.mat')
raw_data=desisl2_rawdata;
temp=raw_data;
temp(:,1:2)=[];


%identify any subjects who gave no resonse
[rows,cols]=size(temp);
delete=[];
for i=1:rows
    notempty=not(cellfun('isempty',temp(i,:)));
    if sum(notempty)~=8
        delete=[delete,i];
    end
end

%create an index of the blank rows 
ctr=1;
delete_idx=[];
for j=1:rows
    if j==delete(ctr);
        delete_idx(j)=0;
        if ctr<length(delete)
        ctr=ctr+1;
        end
    else delete_idx(j)=1;
    end
end
delete_idx=delete_idx';


%create a new dataset eliminating those subjects
full_clean_data=raw_data;
full_clean_data=full_clean_data(delete_idx==1,:);

[rows,cols]=size(full_clean_data);
clean_data=full_clean_data(:,3:cols); %this is the clean data excluding the duration and captcha score. This matches the number of columns in var_names below. 

%remove first two cols of var_names because they are NaN (must add 2 to any
%index derived from var_names when referring to full_clean_data)
var_names=var_names(:,3:length(var_names));

%%
%Collect the data
fulldata=[];
fullcelldata={};
[rows,cols]=size(clean_data);

for cursub=1:rows
    %1. add subnum to full data
    fulldata(cursub,1)=cursub;
    fullcelldata{cursub,1}=cursub;
    %2. add duration to full data
    fulldata(cursub,2)=full_clean_data{cursub,1};
    %3. add captcha score to full data
    %fulldata(cursub,3)=full_clean_data{cursub,2};
    
    %for this subject, get the columns where there is data
    cond_index=not(cellfun('isempty',clean_data(cursub,:)));
    sub_conds=var_names(:,cond_index);
    sub_data=clean_data(cursub,cond_index);
    
    %find the condition
    cond_array={'LLH','LHH'};
    for c=1:length(cond_array)
        cur_cond=strfind(sub_conds(1,1),cond_array{c});
        if not(cellfun('isempty',cur_cond));
            fulldata(cursub,4)=c; %4. set fulldata column 4 to condition number
            fullcelldata{cursub,2}=cond_array{c};
            condition=cond_array{c};
            cond_num=c;
        end
    end
    
    %%
    %Set the critical information for each condition
    %identify the right, scenario specific condition matrix and specify
    %keywords to identify different actions
    norm1='axe';
    norm2='rod';
    norm3='filter';
    abnorm1='ring';
    abnorm2='polish';
    abnorm3='stuffed';
    
    if cond_num==1
        cond_mat=LLH;
    elseif cond_num==2
        cond_mat=LHH;
    end
    
    %Identify the right row in the condition matrix
    [r,c]=size(cond_mat);
    for currow=1:r
        rowcheck=strfind(sub_conds(1,1),[num2str(currow),'_',condition]);%find the first number that comes before the condition name in the variable list
        if not(cellfun('isempty',rowcheck)); %if there is a number there
            rownum=currow; %set this as the row number in the condition matrix for this subject.
        end
    end

   
    %%
    %5. Get the actual and alternative actions 
    %get the actual item
    act_check=strfind(cond_mat(rownum,6),abnorm1); %look in the actual action column for the first specific abnormal actions
    if not(cellfun('isempty',act_check)) %if it is there
            act_action=abnorm1; %set the actual action to this action
    else
        act_check=strfind(cond_mat(rownum,6),abnorm2);%look in the actual action column for the second abnormal actions
        if not(cellfun('isempty',act_check)) %if it is there
            act_action=abnorm2; %set the actual action to this action
        else
            act_check=strfind(cond_mat(rownum,6),abnorm3);%look in the actual action column for the second abnormal actions
            if not(cellfun('isempty',act_check)) %if it is there
                act_action=abnorm3; %set the actual action to this action
            end
        end
    end
    
    %get the first alternative item
    if fulldata(cursub,4)==1 %if it is a LLH trial
        alt1_check=strfind(cond_mat(rownum,7),abnorm1);
        if not(cellfun('isempty',alt1_check))
            alt1=abnorm1;
        else
            alt1_check=strfind(cond_mat(rownum,7),abnorm2);%look in the actual action column for the second abnormal actions
            if not(cellfun('isempty',alt1_check)) %if it is there
            alt1=abnorm2; %set the actual action to this action
            else
                alt1_check=strfind(cond_mat(rownum,7),abnorm3);%look in the actual action column for the second abnormal actions
                if not(cellfun('isempty',alt1_check)) %if it is there
                    alt1=abnorm3; %set the actual action to this action
                end
            end
        end
    else
        alt1_check=strfind(cond_mat(rownum,7),norm1);
        if not(cellfun('isempty',alt1_check))
            alt1=norm1;
        else
            alt1_check=strfind(cond_mat(rownum,7),norm2);%look in the actual action column for the second abnormal actions
            if not(cellfun('isempty',alt1_check)) %if it is there
            alt1=norm2; %set the actual action to this action
            else
                alt1_check=strfind(cond_mat(rownum,7),norm3);%look in the actual action column for the second abnormal actions
                if not(cellfun('isempty',alt1_check)) %if it is there
                    alt1=norm3; %set the actual action to this action
                end
            end
        end
    end
    
    %get the second alternative item
    alt2_check=strfind(cond_mat(rownum,8),norm1);
        if not(cellfun('isempty',alt2_check))
            alt2=norm1;
        else
            alt2_check=strfind(cond_mat(rownum,8),norm2);%look in the actual action column for the second abnormal actions
            if not(cellfun('isempty',alt2_check)) %if it is there
            alt2=norm2; %set the actual action to this action
            else
                alt2_check=strfind(cond_mat(rownum,8),norm3);%look in the actual action column for the second abnormal actions
                if not(cellfun('isempty',alt2_check)) %if it is there
                    alt2=norm3; %set the actual action to this action
                end
            end
        end
        
   

    %Put all of the actual and alternative information into fullcelldata
    fullcelldata{cursub,3}=act_action;
    fullcelldata{cursub,4}=alt1;
    fullcelldata{cursub,5}=alt2;

    
    %%
    %Get responses
    %find the col that asks the force question
    force_check=strfind(sub_conds(1,:),'force');
    force_index=not(cellfun('isempty',force_check));
    
    %find the cols that ask the unusual question
    norm_check=strfind(sub_conds(2,:),'unusual');
    norm_index=not(cellfun('isempty',norm_check)); 
    
    %find the cols that ask the value question
    val_check=strfind(sub_conds(2,:),'good idea');
    val_index=not(cellfun('isempty',val_check));
    
    %find the cols that ask the about the critical alternative
    alt1_check=strfind(sub_conds(2,:),'Field-7');
    alt1_index=not(cellfun('isempty',alt1_check));
    
    %find the cols that ask the about the other alternative
    alt2_check=strfind(sub_conds(2,:),'Field-8');
    alt2_index=not(cellfun('isempty',alt2_check));
    
    alt1_normindex=and(norm_index,alt1_index);
    alt2_normindex=and(norm_index,alt2_index);
    
    alt1_valindex=and(val_index,alt1_index);
    alt2_valindex=and(val_index,alt2_index);
    
    
    %find the norm column that asks about alt1 item
    fulldata(cursub,5)=str2num(sub_data{1,alt1_normindex}); %save it to fulldata
    
    %find the norm colum that asks about alt2 item
    fulldata(cursub,6)=str2num(sub_data{1,alt2_normindex}); %save it to fulldata
    
    %find the val column that asks about alt1 item
    fulldata(cursub,7)=str2num(sub_data{1,alt1_valindex}); %save it to fulldata
    
    %find the val colum that asks about alt2 item
    fulldata(cursub,8)=str2num(sub_data{1,alt2_valindex}); %save it to fulldata
    
    %find the force column
    fulldata(cursub,9)=str2num(sub_data{1,force_index}); %save it to fulldata
    
    %%
    %Add comprehension check answers to results
    %check for correct answer to comp check question one (should be field
    %9)
    comp1_check=strfind(sub_data(6),'9');
    if not(cellfun('isempty',comp1_check))
        fulldata(cursub,10)=1;
    else fulldata(cursub,10)=0;
    end
    
    %check for correct answer to comp check question two (should be field 6)
    comp2_check=strfind(sub_data(7),'6');
    if not(cellfun('isempty',comp2_check))
        fulldata(cursub,11)=1;
    else fulldata(cursub,11)=0;
    end
    
    %add open ended comp check question 3 to full cell data
    fullcelldata(cursub,6)=sub_data(8);
end

%Comprehension checks

%remove subjects who failed the comprehension check question 1
finalfulldata=fulldata(fulldata(:,10)==1,:);
finalfullcelldata=fullcelldata(fulldata(:,10)==1,:);

%remove subjects who failed the comprehension check question 2
finalfullcelldata=finalfullcelldata(finalfulldata(:,11)==1,:);
finalfulldata=finalfulldata(finalfulldata(:,11)==1,:);


save finaldata_precomp.mat finalfulldata finalfullcelldata



%%
%%%%%%%%%
%%%%%%%%% NOTE: Sometimes writetable leaves out rows! DOUBLE CHECK%%%%%%%%%
%%%%%%%%%

%At this point, write fullcelldata to csv, check the open ended comp check
%questiosn 3 and manually create a column 7 that is a 1 for correct and a 0
%for incorrect. 
finalfullcelldata_table=cell2table(finalfullcelldata);

if ~exist('exp2b_fullcelldata_comp3.csv')
    writetable(finalfullcelldata_table,'exp2b_fullcelldata_comp3.csv')
end


%Manually edit full cell data, save it as desisl2_fullcelldata_comp3.csv
%and then manually import it and save it as fullcelldata_withcomp.mat
if exist('fullcelldata_withcomp.mat')
    load('fullcelldata_withcomp.mat')
    
    comp3=cell2mat(fullcelldata_withcomp(:,7));
    finalfulldata=finalfulldata(comp3==1,:);
    finalfullcelldata=fullcelldata_withcomp(comp3==1,:);
    
    csvwrite('exp2b_fulldata_foranalysis.csv',finalfulldata);

    finalfullcelldata_table=cell2table(finalfullcelldata);
    writetable(finalfullcelldata_table,'exp2b_fullcelldata_foranalysis.csv');

    [cellrows,cellcols]=size(finalfullcelldata);
    alldata=[num2cell(finalfulldata),finalfullcelldata];
    alldata_table=cell2table(alldata);
    writetable(alldata_table,'exp2b_completefinaldata_foranalysis.csv');

end

